﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Reflection;
using CoreRp.Db_Models;

namespace CoreRp.Services.Migrations.Versions
{
    internal class BaseVersion : IMigration
    {
        public DateTime VersionTime { get; } = new DateTime(2021, 2, 1);


        public void Run(IFreeSql Db)
        {

            #region 同步表结构

            Db.CodeFirst.SyncStructure(typeof(RAP_CONFIG));
            Db.CodeFirst.SyncStructure(typeof(RAP_COMPANY));
            Db.CodeFirst.SyncStructure(typeof(RAP_ROLE));
            Db.CodeFirst.SyncStructure(typeof(RAP_USER));
            Db.CodeFirst.SyncStructure(typeof(RAP_REL_USER_ROLE));


            #endregion

            #region 插入基础数据


            // 基本用户信息
            var cmpny = new RAP_COMPANY()
            {
                COMPANYCODE="SUPER",
                COMPANYNAME="超级企业",
                ENABLE="Y"
            };
            var user = new RAP_USER()
            {
                COMPANYCODE=cmpny.COMPANYCODE,
                ENABLE="Y",
                LOGINNAME="T1",
                PASSWORD="123321".Md5(),
                SEX="男",
                USERCODE="SUPERADMINISNOTAGUID",
                NICKNAME="超管",
                REALNAME="超级管理员"
            };
            var relUserRole = new RAP_REL_USER_ROLE()
            {
                ROLECODE = E_Roles.SUPERADMIN.ToString(),
                USERCODE = user.USERCODE
            };

            Db.InsertOrUpdate<RAP_COMPANY>().SetSource(cmpny).ExecuteAffrows();
            Db.InsertOrUpdate<RAP_USER>().SetSource(user).ExecuteAffrows();
            if(!Db.Select<RAP_REL_USER_ROLE>().Any(x=>x.ROLECODE==relUserRole.ROLECODE && x.USERCODE == relUserRole.USERCODE))
            {
                Db.Insert<RAP_REL_USER_ROLE>(relUserRole).ExecuteAffrows();
            }

            Db.Delete<RAP_CONFIG>().Where(x=>x.CODE==MigrationManager.ConfigCode && x.BELONG==MigrationManager.ConfigType && x.COMPANYCODE=="SUPER").ExecuteAffrows();
            Db.Insert<RAP_CONFIG>(new RAP_CONFIG()
            {
                //CODE = Guid.NewGuid().ToString(),
                CODE = MigrationManager.ConfigCode,
                VALUE = VersionTime.ToOADate().ToString(),
                BELONG = MigrationManager.ConfigType,
                ENABLE="Y",
                COMPANYCODE="SUPER",
                NAME=nameof(BaseVersion),
                REMARK="禁止修改，版本记录"
            }).ExecuteAffrows();

            #endregion


        }

        public void RollBack(IFreeSql Db)
        {
            //var config = Db.Select<RAP_CONFIG>().Where(x => x.CODE == MigrationManager.ConfigCode && x.BELONG == MigrationManager.ConfigType && x.VALUE == VersionTime.ToOADate().ToString()).First();
            //if (config == null)
            //{
            //    Console.WriteLine("版本未写入，不执行回滚");
            //    return;
            //}

            Db.Delete<RAP_COMPANY>().Where(x => 1 == 1).ExecuteAffrows();
            Db.Delete<RAP_USER>().Where(x => 1 == 1).ExecuteAffrows();
            Db.Delete<RAP_REL_USER_ROLE>().Where(x => 1 == 1).ExecuteAffrows();

            Db.Delete<RAP_CONFIG>().Where(x => x.CODE == MigrationManager.ConfigCode && x.BELONG == MigrationManager.ConfigType && x.VALUE == VersionTime.ToOADate().ToString()).ExecuteAffrows();

        }

    }
}
